#!/usr/bin/python
# coding=utf-8
   
"""批量打包的bat

python xbale_bat.py -o pp -c waps -x 1 -i 2 -a not -u nes -l com.fcgo.mxd2 -b path
"""
__author__ = 'x'
__email__ = 'x@gmail.com' 
VERSION = "v1.0"

import os, sys, re, time, getopt, string, datetime
import shutil
import glob
import Image
import ConfigParser

tihuan = []
info = []

opts, args = getopt.getopt(sys.argv[1:], "hb:p:c:d:v:w:o:a:i:x:u:l:", ["help", "base=", "packname=", "umeng_channel=", "datetime_sluice=", "version_code=", "version_name=", "do=", "apk_type=", "dir_type=", "append=", "app_type=", "old_packname="])
#基目录
is_has_base_path = False
base_path = ""
#要变成的包名
is_has_packname = False
packname = ""
#渠道
is_has_umeng_channel = False
umeng_channel = ""
#日期,默认30天后
is_has_datetime_sluice = False
datetime_sluice = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time() + 30*24*60*60))
#versionCode
is_has_version_code = False
version_code = time.strftime('%Y%m%d',time.localtime(time.time()))
#versionName
is_has_version_name = False
y_version_name = str(int(time.strftime('%Y',time.localtime(time.time()))) - 2013 + 1)
m_version_name = str(int(time.strftime('%m',time.localtime(time.time()))))
d_version_name = str(int(time.strftime('%d',time.localtime(time.time()))))
version_name = y_version_name+"."+m_version_name+"."+d_version_name
#do something
doit = "pp"
#类型::["bg", "dg", ...]  百度谷歌、多盟谷歌  ...
apk_type = "bg"
#1-单个，2-批量
dir_type = "1"
#追加，append等于“1”表示不追加
append = "1"
#app类型
is_has_app_type = False
app_type = "gba"
#母包名
is_has_old_packname = False
old_packname = "com.androidemu.gbaeurdbzb"

#需要替换的文件后缀
ext_name = ""



def usage():
    print ("Usage:xbale_bat.py [ops]")

if not opts:
    usage()
    sys.exit()
else:
    for op, value in opts:
#        print "op:value="+op+":"+value
        if op == "-b" or op == "--base":
            is_has_base_path = True
            base_path = value
        elif op == "-p" or op == "--packname":
            is_has_packname = True
            packname = value
        elif op == "-c" or op == "--umeng_channel":
            is_has_umeng_channel = True 
            umeng_channel = value
        elif op == "-d" or op == "--datetime_sluice":
            is_has_datetime_sluice = True 
            datetime_sluice = value
        elif op == "-v" or op == "--version_code":
            is_has_version_code = True
            version_code = value
        elif op == "-w" or op == "--version_name":
            is_has_version_name = True
            version_name = value
        elif op == "-o" or op == "--do":
            doit = value
        elif op == "-a" or op == "--apk_type":
            apk_type = value        
        elif op == "-i" or op == "--dir_type":
            dir_type = value
        elif op == "-x" or op == "--append":
            append = value        
        elif op == "-u" or op == "--app_type":
            is_has_app_type = True
            app_type = value
        elif op == "-l" or op == "--old_packname":
            is_has_old_packname = True
            old_packname = value           
        elif op == "-h" or op == "--help":
            usage()            
            sys.exit()
            

def main():
    '''主函数'''
    ngapktool_path = "F:\\wwplan\\hack\\tool\\NgApkTool"
    ini_file_name = "p.ini"
    
    if not os.path.exists(base_path):
        print "root path not exists"
        sys.exit()

    dir_list = []
    dir_name_list = []
    bad_item_list = []
    bad_item_name_list = []
    #判断是否是批量
    if dir_type == "2":
        #扫描目录
        for i in os.listdir(base_path):
            item_path = base_path + "\\" + i;
            if not os.path.isdir(item_path):
                bad_item_list.append(item_path)
                bad_item_name_list.append(i)
                continue
            if not os.path.isfile(item_path+"\\"+ini_file_name):
                bad_item_list.append(item_path)
                bad_item_name_list.append(i)                
                continue
            dir_list.append(item_path)
            dir_name_list.append(i)
    elif dir_type == "1":
        dir_list.append(base_path)
        dir_name_list.append(base_path)

    i = 0
    for item_path in dir_list:
        shells = []
        print "do..."+dir_name_list[i]
        if doit == "all":
            shells.append('python '+ngapktool_path+'\\xbale.py -b %s -o %s -c %s -x %s' % (item_path, "pp", umeng_channel, append))
            shells.append('python '+ngapktool_path+'\\xbale.py -b %s -o %s -c %s -x %s' % (item_path, "dj", "hiapk", append))
        else:
            if doit == "dj":
                shells.append('python '+ngapktool_path+'\\xbale.py -b %s -o %s -c %s -x %s' % (item_path, "dj", "hiapk", append))
            else:
                if umeng_channel != "":
                    shells.append('python '+ngapktool_path+'\\xbale.py -b %s -o %s -c %s -x %s' % (item_path, doit, umeng_channel, append))
                else:
                    shells.append('python '+ngapktool_path+'\\xbale.py -b %s -o %s -x %s' % (item_path, doit, append))
        for shell in shells:
            if is_has_version_code:
                shell = shell+' -v %s -w %s'%(version_code, version_name)
            os.system(shell+' -a %s -u %s -l %s' % (apk_type, app_type, old_packname))          
        print dir_name_list[i]+" bale is ok!"
        i = i + 1;
    if bad_item_name_list:
        print "not bale item:"
        for name in bad_item_name_list:
            print name
    
    print "Xbale bat is Complete!"

if __name__ == "__main__":
  sys.exit(main())
